import http from '../../apis/api.js';
import dateTimePicker from '../../utils/dateTimePicker.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    type:'',
    imageList: [],/**图片列表 */
    detail:{},
    formData: {
      pid:'',
      machine:'',
      machineName:'',/**机器编号**/
      startTime:'',/**开始保养时间 */
      endTime:'',/**结束保养时间 */
      result:0,/**0-保养未完成 1-保养已完成 */
      remark: '',/**保养结果说明 */
    },
    error: {
      machineName:'',
    },
    submitDisable: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.id = options.id ? options.id:null;
    if (self.data.id){
      self.data.type='edit'
      wx.setNavigationBarTitle({
        title: '编辑保养单',
      })
      self.getMaintainDetail()
    }else{
      self.data.type = 'add'
      wx.setNavigationBarTitle({
        title: '新增保养单',
      })
      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
      var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
      self.data.detail.startTimeArray = obj1.dateTime;
      self.data.detail.endTimeArray = obj2.dateTime;
      self.setData({
        dateTimeArray1: obj1.dateTimeArray,
        detail: self.data.detail
      })
    }
    self.setData({
      type: self.data.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getMaintainDetail(){
    http.maintainDetail(self.data.id).then(res=>{
      self.data.detail = res.content;
      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, self.data.detail.startTime);
      var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, self.data.detail.endTime);
      self.data.detail.startTimeArray = obj1.dateTime;
      self.data.detail.endTimeArray = obj2.dateTime;
      self.data.formData.pid = res.content.pid;
      self.data.formData.machine = res.content.machine;
      self.data.formData.machineName = `${res.content.shCustomerInfo.name}-${res.content.machineName.type}-${res.content.machineName.macCode}`;
      self.data.formData.startTime = self.data.detail.startTime;/**开始保养时间 */
      self.data.formData.endTime = self.data.detail.endTime;/**开始保养时间 */
      self.data.formData.result = self.data.detail.result;/**开始保养时间 */
      self.data.formData.remark = self.data.detail.remark;/**开始保养时间 */
      self.data.detail.picture1 ? self.data.imageList.push(self.data.detail.picture1):''; 
      self.data.detail.picture2 ? self.data.imageList.push(self.data.detail.picture2) : ''; 
      self.data.detail.picture3 ? self.data.imageList.push(self.data.detail.picture3) : ''; 
      self.setData({
        dateTimeArray1: obj1.dateTimeArray,
        detail: self.data.detail,
        formData: self.data.formData,
        imageList: self.data.imageList
      })
    })
  },
  /**修改时间 */
  changeDateTime1(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case "startTime":
        self.data.formData.startTime = `${self.data.dateTimeArray1[0][e.detail.value[0]]}-${self.data.dateTimeArray1[1][e.detail.value[1]]}-${self.data.dateTimeArray1[2][e.detail.value[2]]} ${self.data.dateTimeArray1[3][e.detail.value[3]]}:${self.data.dateTimeArray1[4][e.detail.value[4]]}:${self.data.dateTimeArray1[5][e.detail.value[5]]}`;
        break;
      case "endTime":
        self.data.formData.endTime = `${self.data.dateTimeArray1[0][e.detail.value[0]]}-${self.data.dateTimeArray1[1][e.detail.value[1]]}-${self.data.dateTimeArray1[2][e.detail.value[2]]} ${self.data.dateTimeArray1[3][e.detail.value[3]]}:${self.data.dateTimeArray1[4][e.detail.value[4]]}:${self.data.dateTimeArray1[5][e.detail.value[5]]}`
        break;
    }
    self.setData({ formData: self.data.formData });
  },
  /**列改变时触发*/
  changeDateTimeColumn1(e) {
    let type = e.currentTarget.dataset.type;
    var arr, dateArr = this.data.dateTimeArray1;
    switch (type) {
      case "startTime":
        arr = self.data.detail.startTimeArray;
        break;
      case "endTime":
        arr = self.data.detail.endTimeArray;
        break;
    }
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      detail: self.data.detail
    });
  },
  /**点击选择处理结果 */
  selectRadio(e) {
    self.data.formData.result = Number(e.currentTarget.dataset.itemvalue);
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'machineName':
        if (self.data.formData.machineName === '') {
          self.data.error.machineName = "机器信息不能为空"
          return false
        }
        self.data.error.machineName = '';
        return true;
        break;
    }
  },
  /**获取input的值*/
  getValus(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'remark':
        self.data.formData.remark = e.detail.value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  submitInfo() {
    self.setData({
      submitDisable: true
    })
    if (self.checked('machineName')) {
      let type = '';
      let postData = {
        pid: self.data.formData.pid,
        machine: self.data.formData.machine,
        startTime: self.data.formData.startTime,
        endTime: self.data.formData.endTime,
        result: self.data.formData.result,
        remark: self.data.formData.remark
      };
      if (self.data.id) {
        type='update';
        postData.id = self.data.id;
      }else{
        type = 'insert'
      }
      let imageList = this.selectComponent('#imageId').data.imageList; // 页面获取自定义组件实例
      if (imageList.length > 0) {
        imageList.forEach((item, index) => {
          postData['picture' + (index + 1)] = item.path;
        })
      }
      http.addUpdateMaintain(type,postData).then(res => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success() {
            wx.navigateBack();
          },
          complete() {
            self.setData({
              submitDisable: false
            })
          }
        })
      }, error => {
        self.setData({
          submitDisable: false
        })
      })
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  },
  scanMachineCode() {
    wx.scanCode({
      success(res) {
        self.getMachineByMacCode({
          macCode: res.result
        })
      }
    })
  },
  /**根据机器macCode查询机器信息 */
  getMachineByMacCode(data) {
    http.getMachineByMacCode(data).then(res => {
      self.data.formData.machineName = `${res.content.plasmaStationName}-${res.content.type}-${res.content.macCode}`;
      self.data.formData.pid = res.content.pid;
      self.data.formData.machine = res.content.id;
      self.setData({
        formData :self.data.formData
      })
    })
  }
})